/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chatNI;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.Socket;

/**
 *
 * @author anthony
 */
public class TCPRec implements Runnable {

    private Socket soc;
    private String chemin;
    private InetAddress address;

    public TCPRec(int port, InetAddress address, String chemin) throws IOException {
        this.soc = new Socket(address, port);
        this.chemin = chemin;
        this.run();
    }

    @Override
    public void run() {
        try {
            FileOutputStream fos = new FileOutputStream(chemin);
            BufferedOutputStream out = new BufferedOutputStream(fos);
            byte[] buffer = new byte[1024];
            int count;
            InputStream in = soc.getInputStream();
            while ((count = in.read(buffer)) >= 0) {
               out.write(buffer, 0, count);
            }
            fos.close();
            soc.close();
        } catch (Exception e) {
            System.err.println(e);
        }
    }
}
